package com.gibs.peixunapi.dao;

import java.util.List;

import com.gibs.peixunapi.model.SubjectHub;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.gibs.peixunapi.model.TestPaperSubject;

@Repository
public interface TestPaperSubjectDao extends JpaRepository<TestPaperSubject, Integer>, JpaSpecificationExecutor<TestPaperSubject> {

    @Query("from TestPaperSubject where subjectHubId = ?1")
    List<TestPaperSubject> find(String name);

    @Query("from TestPaperSubject where subjectHubId like ?1")
    List<TestPaperSubject> findLike(String name);

    @Query(value = "select * from TestPaperSubject limit ?, ?", nativeQuery = true)
    List<TestPaperSubject> findPage(Integer offset, Integer size);

    @Query(value = "select * from TestPaperSubject where subjectHubId = ? limit ?, ?", nativeQuery = true)
    List<TestPaperSubject> findPage(String name, Integer offset, Integer size);

    @Query(value = "select * from TestPaperSubject where subjectHubId like ? limit ?, ?", nativeQuery = true)
    List<TestPaperSubject> findPageLike(String name, Integer offset, Integer size);

    @Query(value = "select count(*) from TestPaperSubject where subjectHubId = ?", nativeQuery = true)
    Long count(String name);

    @Query(value = "select count(*) from TestPaperSubject where subjectHubId like ?", nativeQuery = true)
    Long countLike(String name);

    @Query("from TestPaperSubject order by id asc")
    List<TestPaperSubject> findAllAsc();

    @Query("from TestPaperSubject where testPaper.id = ?1 order by subjectHub.type,sort")
    List<SubjectHub> findSubjectHubListByTestPaperId(Integer testPaperId);

    @Query(value = "update test_paper_subject set subject_hub_id = ?1 where test_paper_id = ?2 and subject_hub_id = ?3", nativeQuery = true)
    Integer updateTestPaperSubject(Integer newSubjectHubId, Integer testPaperId, Integer oldSubjectHubId);
}

